/* the timepicker, used by kalendar and calendar.cfm */
Time = {
	init:function(){
		$timepickers = $('.timepicker-hour td,.timepicker-ampm td,.timepicker-minutes td').not('selected');
		$timepickers.off();
		$timepickers.on('click',function(){
			Time.select(this);
		});
	},
	formatTime:function(hh,mm){ // takes military time (string or int) and outputs humanized version
		var hour = +hh;	var ampm = 'AM';
		if(hour > 11){
			ampm = 'PM';
			hour = hour - 12;
		}
		if(hour == 0){
			hour = 12;
		}
		return hour + ':' + mm + ' ' + ampm;
	},
	select:function(that){
		var $this = $(that);
		var $row = $this.parent();
		var $timepicker = $row.closest('.timepicker');
		$row.find('td.selected').removeClass('selected');
		$this.addClass('selected');
		Time.calculateTime($timepicker);
		Time.filterAfter($timepicker);
	},
	calculateTime:function($timepicker){
		var pm = false;
		if($timepicker.find('.timepicker-ampm td.selected').text() == 'PM'){		pm = true;}
		var hh = +$timepicker.find('.timepicker-hour td.selected').attr('value');
		if(pm){		hh += 12;}
		if(hh < 10){	hh = '0' + hh;}
		var mm = +$timepicker.find('.timepicker-minutes td.selected').attr('value');
		if(mm < 10){	mm = '0' + mm;}
		var result = {};
		result.formattedTime = Time.formatTime(hh,mm);
		result.time = hh + ':' + mm + ':00';
		$timepicker.find('button:last').text('use '+result.formattedTime);
		return result;
	},
	setTime:function(that){
		var $this = $(that);
		var $timepicker = $this.closest('.timepicker');
		var $event = $this.closest('.event')
		var $input = $timepicker.find('input');
		var name_uuid = $timepicker.attr('id');
		var $button = $event.find('[name='+name_uuid+']').find("span");
		var result = Time.calculateTime($timepicker);
		var show = $timepicker.attr('show');
		var stopname = $timepicker.attr('stopname');
		
		$timepicker.addClass('hidden');
		$timepicker.parent().find(show).removeClass('hidden');
		
		$button.text(result.formattedTime);
		$input.val(result.time);
		if(stopname.length > 0){
			Time.filterStoptime($timepicker,stopname);
		}
	},
	cancelSetTime:function(that){
		var $this = $(that);
		var $timepicker = $this.closest('.timepicker');
		var show = $timepicker.attr('show');
		$timepicker.addClass('hidden');
		$timepicker.parent().find(show).removeClass('hidden');
	},
	filterStoptime:function($timepicker,stopname){
		var starttimeArray = $timepicker.find('input').val().split(':');
		var $parent = $timepicker.parent();
		var stoptimeArray = $parent.find('input[name='+stopname+']').val().split(':');
		var $stopTimepicker = $parent.find('input[name='+stopname+']').parent();
		var starttime = starttimeArray[0] + starttimeArray[1];
		var stoptime = stoptimeArray[0] + stoptimeArray[1];
		starttime = +starttime;
		stoptime = +stoptime;
		if(stoptime < starttime){
			Time.setTimepickerTime($stopTimepicker,starttimeArray[0],starttimeArray[1]);
		}
		$stopTimepicker.attr('after',starttimeArray[0]+":"+starttimeArray[1]);
		Time.filterAfter($stopTimepicker);
	},
	setTimepickerTime:function($timepicker,hh,mm){
		var am = true;
		var hh = +hh;
		if(hh > 11){
			hh = hh - 12;
			am = false;
		}
		var mm = +mm;
		$timepicker.find('.selected').removeClass('selected');
		$timepicker.find('.timepicker-hour td[value='+hh+']').addClass('selected');
		$timepicker.find('.timepicker-minutes td[value='+mm+']').addClass('selected');
		if(am){
			$timepicker.find('.timepicker-am').addClass('selected');
		} else {
			$timepicker.find('.timepicker-pm').addClass('selected');
		}
		var button = $timepicker.find('button').get(0);
		Time.setTime(button);
	},
	filterAfter:function($timepicker){
		var after = $timepicker.attr('after') || "";
		$timepicker.find('.vis-hidden').removeClass('vis-hidden');
		var $pm = $timepicker.find(".timepicker-pm");
		if(after != ""){
			var afterArray = after.split(':');
			var after_hh = afterArray[0];
			var after_mm = afterArray[1];
			var after_h = +after_hh;
			var after_m = +after_mm;
			
			var current_h = +$timepicker.find('.timepicker-hour td.selected').attr('value');
			var current_m = +$timepicker.find('.timepicker-minutes td.selected').attr('value');
			
			// handle ampm
			if(after_h > 11){
				$timepicker.find('.timepicker-am').addClass('vis-hidden');
			}
			// handle hours based on ampm
			var hourmod = 0;
			if($pm.hasClass('selected')){
				hourmod = 12;
			}
			$timepicker.find('.timepicker-hour td').each(function(){
				var val = +$(this).attr('value');
				val = val + hourmod;
				if(after_h > val){
					$(this).addClass('vis-hidden');
				}
			});
			// handle minutes based on hour
			if(current_h == after_h){
				$timepicker.find('.timepicker-minutes td').each(function(){
					var $this = $(this);
					var val = +$this.attr('value');
					if(after_m > val){
						$this.addClass('vis-hidden');
						if($this.hasClass('selected')){
							Time.select($this.next().get(0));
						}
					}
				});
			}
		}
	},
	allDayBaby:function(that){
		var $this = $(that);
		var $event = $this.closest('.event');
		var $startpicker = $event.find('.timepicker[name=start]');
		var $stoppicker = $event.find('.timepicker[name=stop]');
		$startpicker.find('.td.selected');
		Time.setTimepickerTime($startpicker,'00','00');
		Time.setTimepickerTime($stoppicker,'23','55');
		Calendar.showStop($event.find('.event-show-stop').get(0));
		Time.setTime($stoppicker.find('.timepicker-set-time').get(0));
		$event.find('.button-stop-text,.button-start-text').text('all day');
	}
};